#!/usr/bin/env python

import sys
import os
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *


class DotForm(QDialog):


    def  showflowchart(self):
        self.dotflowchart = QtGui.QGroupBox("current flow chart :")
        layout = QtGui.QHBoxLayout()

        self.flowchartLabel = QtGui.QLabel('')
        self.flowchartLabel.setMinimumSize(700,400)
        layout.addWidget(self.flowchartLabel)

        self.dotflowchart.setLayout(layout)
         
    def  showdotinput(self):

        self.dotinput = QtGui.QGroupBox("input your dot :")
        layout = QtGui.QHBoxLayout()
        initcode = 'digraph test { \r\n '+  \
                  'main ->process; \r\n ' + \
                   'process -> end; \n\r ' + \
                  '}' 
        self.editor = QtGui.QTextEdit('')
        self.editor.append(initcode)
        self.editor.setMinimumSize(300,400)
        layout.addWidget(self.editor)
        self.editor.show()
        self.dotinput.setLayout(layout)
         
    def  timeout(self):

        
        mycode = 'echo "%s" > ./dot/xmeter.dot'%(self.editor.toPlainText())
        print mycode
        os.popen(mycode)
        cmd = 'dot -T png ./dot/xmeter.dot  > ./dot/dot.png'
        os.popen(cmd)
        self.flowchartLabel.setPixmap(QPixmap.fromImage(QImage('./dot/dot.png')).scaled(700,400))


    def __init__(self, parent=None):
        super(DotForm, self).__init__(parent)

        self.showflowchart()
        self.showdotinput()

        mainLayout = QGridLayout()
        
        self.setWindowTitle("My dot")

        mainLayout.addWidget(self.dotflowchart,0,1)
        mainLayout.addWidget(self.dotinput,0,0)
        
        self.setLayout(mainLayout)

        self.timer = QTimer()
        self.connect(self.timer, SIGNAL('timeout()'), self.timeout)
        self.timer.start(1000);

        self.show()


app = QApplication(sys.argv)
form = None
form = DotForm()
sys.exit(app.exec_())




